extends HBoxContainer

@onready var actionScene = preload("res://pages/action/action.tscn")
@export var scheduleData:ScheduleData

@export var scheduleState:GlobalVariables.ScheduleState = GlobalVariables.ScheduleState.UNPROCESSED

@export var unprocessed_texture:Texture2D
@export var delayed_texture:Texture2D
@export var finished_texture:Texture2D
@export var unfinished_texture:Texture2D
@export var unstarted_texture:Texture2D

@onready var finishedstateTag = %state_tag
@onready var scheduleLabel = %scheduleNameLabel
@onready var timeLabel = %time_label
@onready var ability_link_btn = %ability_link_btn

@onready var schedule_progressbar = %ProgressBar
@onready var progress_label = %progress_label

func _ready():
	if scheduleData!=null:
		set_data()
	
func set_data():
	
	set_state_tag()
	scheduleLabel.text = scheduleData.schedule_title
	timeLabel.text = '%d分钟' % scheduleData.length
	var related_ability = GlobalVariables.get_ability(GlobalVariables.load_task(scheduleData.related_task).related_ability)
	ability_link_btn.text = "#"+related_ability.name
	ability_link_btn.add_theme_color_override("font_color",related_ability.color)
	var progress = int(ceil(scheduleData.progress/float(scheduleData.length*60)*100))
	schedule_progressbar.value = progress
	progress_label.text = str(progress)+'%'


func set_state_tag():
	var state = GlobalVariables.check_schedule_state(scheduleData)
	if state == GlobalVariables.ScheduleState.UNPROCESSED:
		finishedstateTag.texture = unprocessed_texture
	elif state == GlobalVariables.ScheduleState.FINISHED:
		finishedstateTag.texture = finished_texture
	elif state == GlobalVariables.ScheduleState.UNFINISHED:
		finishedstateTag.texture = unfinished_texture
	elif state == GlobalVariables.ScheduleState.DELAYED:
		finishedstateTag.texture = delayed_texture
	elif state == GlobalVariables.ScheduleState.UNSTARTED:
		finishedstateTag.texture = unstarted_texture
	else:
		finishedstateTag.texture = unprocessed_texture
		
				

func _on_view_action_btn_pressed():
	Signalbus.new_action_created.emit(ActionData.new('开始行动',actionScene,0,{"scheduleData":scheduleData}))
